package com.bcbim.api_web.space_type.controller;

import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.common.util.ParamCheck;
import com.bcbim.operation_maintenance_interface.space.dto.SpaceUnassignedDTO;
import com.bcbim.operation_maintenance_interface.space.service.SpaceService;
import com.bcbim.operation_maintenance_interface.space_type.dao.SpaceTypeDAO;
import com.bcbim.operation_maintenance_interface.space_type.dto.SpaceTypeUnassignedDTO;
import com.bcbim.operation_maintenance_interface.space_type.service.SpaceTypeService;
import com.bcbim.operation_maintenance_interface.space_type.vo.SpaceTypeVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @version V 1.0.0
 * @Description：空间类型controller
 * @Author: haomeijie
 * @Date: 2022/3/5 13:34
 **/
@RestController
@RequestMapping("/spaceType")
public class SpaceTypeController {
    @Reference(protocol = "dubbo", group = "spaceType", version = "1.0.0")
    private SpaceTypeService spaceTypeServiceImpl;
    @Reference(protocol = "dubbo", group = "space", version = "1.0.0")
    private SpaceService spaceServiceImpl;

    /**
     * 新增空间类型
     *
     * @param spaceTypeDAO
     * @author xuehao
     * @createDate 2022/3/9 17:44
     * @retuen ReturnMsg
     */
    @RequestMapping("/insert")
    public ReturnMsg insert(SpaceTypeDAO spaceTypeDAO) {
        //参数校验
        try {
            ParamCheck.idCheck(spaceTypeDAO.getSpaceCategoryId());
            ParamCheck.stringNotEmpty(spaceTypeDAO.getSpaceType(), 20);
            ParamCheck.idCheck(spaceTypeDAO.getProjectId());
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //类别名称不能重复
        if (spaceTypeServiceImpl.checkSpaceType(spaceTypeDAO.getSpaceType(), spaceTypeDAO.getSpaceCategoryId(), null) > 0) {
            return ReturnMsg.consFail("空间类型重复");
        }
        //整理参数
        spaceTypeDAO.setId(GuidUtil.get())
                .setCreateBy(AuthUtil.getUserIdDecrypt())
                .setCreateTime(LocalDateTime.now())
                .setDel(Constant.NOT_DEL);
        //调用服务
        int i = spaceTypeServiceImpl.insertSpaceType(spaceTypeDAO);
        return i == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }

    /**
     * 编辑类型
     *
     * @param spaceTypeDAO
     * @author xuehao
     * @createDate 2022/3/9 17:46
     * @retuen ReturnMsg
     */
    @PostMapping("/update")
    public ReturnMsg update(SpaceTypeDAO spaceTypeDAO) {
        //参数校验
        try {
            ParamCheck.idCheck(spaceTypeDAO.getId());
            ParamCheck.idCheck(spaceTypeDAO.getSpaceCategoryId());
            ParamCheck.stringNotEmpty(spaceTypeDAO.getSpaceType(), 20);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //类别名称不能重复
        if (spaceTypeServiceImpl.checkSpaceType(spaceTypeDAO.getSpaceType(), spaceTypeDAO.getProjectId(), spaceTypeDAO.getId()) > 0) {
            return ReturnMsg.consFail("空间类型重复");
        }
        //整理参数
        spaceTypeDAO.setUpdateBy(AuthUtil.getUserIdDecrypt())
                .setUpdateTime(LocalDateTime.now());
        //调用服务
        int i = spaceTypeServiceImpl.updateSpaceType(spaceTypeDAO);
        return i == 1 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }

    /**
     * 删除空间类型
     *
     * @param id
     * @author xuehao
     * @createDate 2022/3/10 13:29
     * @retuen ReturnMsg
     */
    @PostMapping("/delete")
    public ReturnMsg delete(String id, String projectId) {
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //查询空间类型详情，修改未指派使用
        SpaceTypeVO type = spaceTypeServiceImpl.getSpaceType(id);
        //整理参数
        SpaceTypeDAO spaceTypeDAO = new SpaceTypeDAO();
        spaceTypeDAO.setDel(Constant.DEL);
        spaceTypeDAO.setUpdateTime(LocalDateTime.now());
        spaceTypeDAO.setUpdateBy(AuthUtil.getUserIdDecrypt());
        spaceTypeDAO.setId(id);
        int result = spaceTypeServiceImpl.updateSpaceType(spaceTypeDAO);
        //清空指定空间的类别
        //查询类型未指派
        //获取类型未指派id
        SpaceTypeUnassignedDTO typeUnassignedDTO = new SpaceTypeUnassignedDTO();
        typeUnassignedDTO.setProjectId(projectId);
        typeUnassignedDTO.setSpaceCategoryId(type.getSpaceCategoryId());
        typeUnassignedDTO.setUnassignedName(Constant.UNASSIGNED);
        String typeUnassignedId = spaceTypeServiceImpl.getUnassignedId(typeUnassignedDTO);
        if (StringUtils.isEmpty(typeUnassignedId)) {
            //如果为空，生成类型未指派数据
            SpaceTypeDAO spaceType = new SpaceTypeDAO();
            typeUnassignedId = GuidUtil.get();
            spaceType.setId(typeUnassignedId)
                    .setProjectId(projectId)
                    .setSpaceCategoryId(type.getSpaceCategoryId())
                    .setSpaceType(Constant.UNASSIGNED)
                    .setCreateBy(AuthUtil.getUserIdDecrypt())
                    .setCreateTime(LocalDateTime.now())
                    .setDel(Constant.NOT_DEL);
            spaceTypeServiceImpl.insertSpaceType(spaceType);
        }
        //整理参数
        SpaceUnassignedDTO spaceUnassignedDTO = new SpaceUnassignedDTO();
        spaceUnassignedDTO.setTypeId(id)
                .setUnassignedId(typeUnassignedId);
        spaceServiceImpl.updateSpaceType(spaceUnassignedDTO);
        return result == 1 ? ReturnMsg.consSuccess(Constant.DEL_SUCCESS) : ReturnMsg.consFail(Constant.DEL_FAIL);
    }

    /**
     * 查询空间类型详情
     *
     * @param id
     * @author xuehao
     * @createDate 2022/3/10 13:40
     * @retuen ReturnMsg
     */
    @PostMapping("/detail")
    public ReturnMsg detail(String id) {
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }

        SpaceTypeVO spaceTypeVO = spaceTypeServiceImpl.getSpaceType(id);
        return ReturnMsg.consSuccess(spaceTypeVO);
    }
}
